package com.bawei.module_play.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.RoutePath
import com.bawei.lib_common.mvi.BaseMviFragment
import com.bawei.module_play.R
import com.bawei.module_play.adapter.PlayListAdapter
import com.bawei.module_play.adapter.PlayLoveAdapter
import com.bawei.module_play.databinding.FragmentIntroduceBinding
import com.bawei.module_play.entriy.AuthorEntriy
import com.bawei.module_play.entriy.CommentEntriyItem
import com.bawei.module_play.entriy.SimpleVideoEntity
import com.bawei.module_play.intent.PlayVideoIntent
import com.bawei.module_play.state.PlayVideoState
import com.bawei.module_play.vm.PlayViewModel
import com.example.image.glide.GlideImgLoader
import com.luck.picture.lib.utils.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch


@AndroidEntryPoint
@Route(path = RoutePath.PATH_FRAGMENT_DETAIL_INTRODUCE)
class IntroduceFragment : BaseMviFragment<FragmentIntroduceBinding>() {

    @Autowired
    @JvmField var userId: String = ""

    @Autowired
    @JvmField var moduleType: Int = 0

    @Autowired
    @JvmField var videoTypeId: Int = -1 //推荐

    @Autowired
    @JvmField var channelId: String = ""

    @Autowired
    @JvmField var title: String = ""

        //viewmodel
    private val mViewModel: PlayViewModel by viewModels()
    private var playloveVideoList= mutableListOf<SimpleVideoEntity>()
    private lateinit var playloveadapter: PlayLoveAdapter


    override fun getLayoutId(): Int {
        return R.layout.fragment_introduce
    }

    override fun initViews() {
        ARouter.getInstance().inject(this)
        mDatatBinding.tvTitle.text=title
       lifecycleScope.launch {
           mViewModel.channel.send(PlayVideoIntent.LoadRecommendSimpleVideo(1,10))
       }
        playloveadapter= PlayLoveAdapter(playloveVideoList)
        mDatatBinding.rvUserVideo.adapter=playloveadapter
    }

    override suspend fun collectUIState() {
        mViewModel.uiStateFlow.collect{
            when(it){
                is PlayVideoState.LoadSimpleVideoSuccess ->updataSimpleVideo(it.list)
                is PlayVideoState.LoadSimpleVideoFail ->{
                    ToastUtils.showToast(activity,"推荐视频加载失败")
                }
                else->{

                }
            }
        }
    }


    private fun updataSimpleVideo(list: List<SimpleVideoEntity>){
        playloveVideoList.addAll(list)
        playloveadapter.notifyDataSetChanged()
    }


}